//
//  QRFQCustomProgressView.m
//  TestProgress
//
//  Created by fq on 2020/10/12.
//

#import "FQChargingProgressView.h"

@interface FQChargingProgressView ()

@property (nonatomic, assign) CGFloat progress;

@end

@implementation FQChargingProgressView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dotWH = 6.0;
        self.dotCount = 36;
        self.sumLayerWH = 300.0;
        self.dotFlagXY = 240.0;
        self.itemCount = 5;
        self.itemScale = 0.97;
        self.itemOffX = -1.0;
        self.itemOffY = -8.0;
        [self getSumViewLayer];
//        self.transform = CGAffineTransformMakeRotation(0.7);
    }
    return self;
}

-(void)getSumViewLayer{

    CALayer * layer = [[CALayer alloc]init];
    layer.frame = CGRectMake(0, 0, self.sumLayerWH, self.sumLayerWH);
    
    layer.backgroundColor = UIColor.clearColor.CGColor;
    layer.position = self.layer.position;
    
    for (int i = 0; i < self.dotCount; i++) {
        CAReplicatorLayer *itemLayer = [self getImageItemView];
        [self.muLayerArr addObject:itemLayer];
        [layer addSublayer:itemLayer];
        CATransform3D transform = CATransform3DIdentity;
        transform = CATransform3DRotate(transform, i * M_PI * 2.0 / self.dotCount, 0, 0, 1);
        itemLayer.transform = transform;
    }
    //旋转角度
    layer.transform = CATransform3DMakeRotation(0.7, 0, 0, 1);
    [self.layer addSublayer:layer];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
}

//首先需要点图
-(CAReplicatorLayer *)getImageItemView{
    
    UIView *dotView = [[UIView alloc]initWithFrame:CGRectMake(self.dotFlagXY, self.dotFlagXY, self.dotWH, self.dotWH)];//这个是最大值
    dotView.backgroundColor = UIColor.whiteColor;
    dotView.layer.cornerRadius = self.dotWH * 0.5;
    
    CAReplicatorLayer *replLayer = [CAReplicatorLayer layer];
    [replLayer addSublayer:dotView.layer];
    replLayer.backgroundColor = UIColor.clearColor.CGColor;
    replLayer.frame = CGRectMake(0, 0, self.sumLayerWH, self.sumLayerWH);//这个是其大小
    replLayer.position = CGPointMake(self.sumLayerWH * 0.5, self.sumLayerWH * 0.5);//中心点
    replLayer.instanceColor = UIColor.whiteColor.CGColor;
    /// 设置复制次数
    replLayer.instanceCount = self.itemCount;
    
    
    CATransform3D transform = CATransform3DIdentity;
    // 偏移
    transform = CATransform3DTranslate(transform, self.itemOffX, self.itemOffY, 0);
    // 缩放
    transform = CATransform3DScale(transform, self.itemScale, self.itemScale, 0);
    //旋转
    transform = CATransform3DRotate(transform, -0.1, 0, 0, -1);
    replLayer.instanceTransform = transform;

    return replLayer;
}

-(void)reloadProgressWithValue:(CGFloat)progress
{
    UIColor * color = UIColor.orangeColor;
    if (progress > 0.2 * self.dotCount) {
        color = UIColor.greenColor;
    }
    //还原之前的颜色
    if (_progress > progress) {
        for (CAReplicatorLayer * itemLayer in self.muLayerArr) {
            itemLayer.instanceColor = UIColor.whiteColor.CGColor;
        }
    }
    
    _progress = progress;
    NSInteger max = ceilf(progress);
    NSInteger min = floorf(progress);
    for (int i = 0; i < max; i++) {
        CAReplicatorLayer * itemLayer = self.muLayerArr[i];
        if (max < 3) {
            itemLayer.instanceColor = [color colorWithAlphaComponent:0.5].CGColor;
        }else{
            
            if (i < max - 2) {
                itemLayer.instanceColor = color.CGColor;
            }else{
                if((i > self.dotCount - 2 && i >= min)||min == self.dotCount){
                    itemLayer.instanceColor = color.CGColor;
                }else{
                    itemLayer.instanceColor = [color colorWithAlphaComponent:0.5].CGColor;
                }
            }
        
        }
    }
}

-(NSMutableArray *)muLayerArr
{
    if (!_muLayerArr) {
        _muLayerArr = [NSMutableArray array];
    }
    return _muLayerArr;
}

@end
